//
//  GYDBottomPopView.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/30.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

class GYDBottomPopView: UIView {
    
    var titleArray : [String]?
    var dissmissBlock :(()->())?
    var selectedBlock : ((index:Int) -> ())?
    var bottomView : UIView?
    
    static func showWithTitleArray(titleArray:[String],selectedBlock:((index:Int) -> ())?,dissmissBlock:(()->())?) {
        let bottomPopView = GYDBottomPopView(titleArray: titleArray, selectedBlock: selectedBlock, dissmissBlock: dissmissBlock)
        UIApplication.sharedApplication().keyWindow?.addSubview(bottomPopView)
    }
    
    init(titleArray:[String],selectedBlock:((index:Int) -> ())?,dissmissBlock:(()->())?) {
        super.init(frame: CGRect(x: 0, y: 0, width: GYDWindowWidth, height: GYDWindowHeight))
        self.titleArray = titleArray
        self.selectedBlock = selectedBlock
        self.dissmissBlock = dissmissBlock
        createViews()
    }
    func createViews() {
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(GYDBottomPopView.dissmiss))
        self.addGestureRecognizer(tap)
        
        
        self.backgroundColor = RGBA(0, g: 0, b: 0, a: 0.5)
        
        let height = CGFloat(((self.titleArray?.count)! + 1) * 50 + 5)
        let bottomView = UIView(frame: CGRect(x: 0, y: GYDWindowHeight, width: GYDWindowWidth, height: height))
        self.bottomView = bottomView
        bottomView.backgroundColor = BASECOLOR
        self.addSubview(bottomView)
        
        for i in 0..<titleArray!.count {
            let button = UIButton(type: UIButtonType.Custom)
            button.setTitle(titleArray![i], forState: UIControlState.Normal)
            button.frame = CGRect(x: 0, y: i*50, width: Int(GYDWindowWidth), height: 50)
            button.backgroundColor = UIColor.whiteColor()
            button.setTitleColor(RGB(70, g: 70, b: 70), forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(17)
            let lineView = UIView(frame: CGRect(x: 0, y: 49.5, width: GYDWindowWidth, height: 0.5))
            lineView.backgroundColor = BASECOLOR
            button.addSubview(lineView)
            button.tag = 80 + i
            button.addTarget(self, action: #selector(GYDBottomPopView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            bottomView.addSubview(button)
        }
        
        
        let button = UIButton(type: UIButtonType.Custom)
        button.setTitle("取消", forState: UIControlState.Normal)
        button.frame = CGRect(x: 0.0, y: bottomView.height - 50, width: GYDWindowWidth, height: 50.0)
        button.backgroundColor = UIColor.whiteColor()
        button.setTitleColor(RGB(70, g: 70, b: 70), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(17)
        button.tag = 101
        bottomView.addSubview(button)
        button.addTarget(self, action: #selector(GYDBottomPopView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        UIView.animateWithDuration(0.35) {
            bottomView.top = GYDWindowHeight - height
        }
        
    }
    @objc func buttonClick(button:UIButton) {
        let tag = button.tag
        if tag == 101 {
            dissmiss()
            return
        }else {
            UIView.animateWithDuration(0.35, animations: {
                self.bottomView!.top = GYDWindowHeight
            }) { (stop) in
                self.removeFromSuperview()
                if self.selectedBlock != nil {
                    self.selectedBlock!(index: tag - 80)
                }
            }
        }
    }
    
    
    @objc func dissmiss() {
        UIView.animateWithDuration(0.35, animations: {
            self.bottomView!.top = GYDWindowHeight
            }) { (stop) in
                if self.dissmissBlock != nil {
                    self.dissmissBlock!()
                }
                self.removeFromSuperview()
        }
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("bootomPopView---Delloc")
    }
    
}
